//
//  AXFShoppingView.swift
//  SpuerYangAngWang
//
//  Created by 杨茂盛 on 2016/12/24.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit

class AXFShoppingView: UIView {

    //添加购物车上的 小圆点
    let ringImageView = UIImageView.init(image: UIImage.init(named: "reddot"))
    
    //记录按钮的点击次数
    var idex:Int = 0 {
        didSet{
            countLabel.text = "\(idex)"
            if self.idex >= 10 {
     
                
            }
        }
    }
    
    //减号按钮
    let minusBtn = UIButton.init()
    //添加计数 lable
    let countLabel = UILabel.init(title: "0", textColor:  #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), fontSize: 14)
    //闭包
    var clickPlusClosure: (() -> ())?

    var clickMinusClosure: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //购物车布局
        self.setUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension AXFShoppingView{
    //搭建界面
    func setUI(){
        //添加商品 label
        let goodsLabel = UILabel.init(title: "添加商品", textColor:  #colorLiteral(red: 0.05882352963, green: 0.180392161, blue: 0.2470588237, alpha: 1), fontSize: 16)
        self.addSubview(goodsLabel)
        //添加减号按钮
        minusBtn.setImage(UIImage.init(named: "v2_reduce"), for: .normal)

        //点击事件
        minusBtn.addTarget(self, action: #selector(clickMinusBtn), for: .touchUpInside)
        self.addSubview(minusBtn)

        self.addSubview(countLabel)
        //添加 加号按钮
        let plusBtn = UIButton.init()
         plusBtn.setImage(UIImage.init(named: "v2_increase"), for: .normal)
        //点击事件
        plusBtn.addTarget(self, action: #selector(clickPlusBtn), for: .touchUpInside)
        self.addSubview(plusBtn)
        
        goodsLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(4)
            make.top.equalTo(30)
        }
        minusBtn.snp.makeConstraints { (make) in
            make.leading.equalTo(goodsLabel.snp.trailing).offset(3)
            make.top.equalTo(goodsLabel)
            make.size.equalTo(CGSize.init(width: 20, height: 20))
        }
        countLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(minusBtn.snp.trailing).offset(3)
            make.centerY.equalTo(minusBtn)
        }
        plusBtn.snp.makeConstraints { (make) in
            make.leading.equalTo(countLabel.snp.trailing).offset(3)
            make.size.equalTo(CGSize.init(width: 20, height: 20))
            make.top.equalTo(goodsLabel)
        }
        
        //添加购物车背景 view
        let shoppingCar = UIView.init(frame: CGRect.init(x: ScreenWidth-70, y: -20, width: 60, height: 60))
        shoppingCar.backgroundColor = #colorLiteral(red: 0.9686274529, green: 0.78039217, blue: 0.3450980484, alpha: 1)
        shoppingCar.layer.cornerRadius = 30
        shoppingCar.layer.masksToBounds = true
        self.addSubview(shoppingCar)

        //添置购物车
        let imageCar = UIImageView.init()
        imageCar.image = UIImage.init(named: "shopCart")
        shoppingCar.addSubview(imageCar)
        imageCar.snp.makeConstraints { (make) in
            make.center.centerY.equalTo(shoppingCar)
            make.size.equalTo(CGSize.init(width: 40, height: 40))
        }
        //添加购物车上的 小圆点
        let redImageView = UIImageView.init(image: UIImage.init(named: "reddot"))
        self.addSubview(redImageView)
        
        //圆点上的计数 label
        let redIMageViewCountLabel = UILabel.init(title: "20", textColor: .black, fontSize: 12)
        redIMageViewCountLabel.textAlignment = .center
        redImageView.addSubview(redIMageViewCountLabel)
        
        redImageView.snp.makeConstraints { (make) in
            make.trailing.top.equalTo(shoppingCar)
            make.size.equalTo(CGSize.init(width: 18, height: 18))
        }
        redIMageViewCountLabel.snp.makeConstraints { (make) in
            make.edges.equalTo(redImageView)
        }
    }
    
    //加号
    func clickPlusBtn(){
        self.idex += 1
        self.clickPlusClosure!()
    }
    //减号
    func clickMinusBtn(){
        self.idex -= 1
        self.clickMinusClosure!()
    }
}
